package com.app.model.bean;

import android.os.Parcel;
import android.os.Parcelable;

import com.app.constant.Constants;
import com.app.utils.StringUtil;

import java.io.Serializable;

/**
 * 类描述：
 */
public class ChoosableImage implements Comparable<ChoosableImage>, Serializable, Parcelable {
    private static final String TAG = "ChoosableImage";
    public boolean choosed;
    public long chooseTime;//选择时间
    public String localImageUri;//图片在本地的Uri地址
    public String localPath;//图片本地路径
    public String cloudKey;//图片在云端的键值
    public boolean uploadSucceed;//是否上传成功
    public int uploadPercent;//上传进度
    public boolean fromCamera;
    public String thumbnailPath;
    public static final Creator<ChoosableImage> CREATOR = new Creator<ChoosableImage>() {

        @Override
        public ChoosableImage createFromParcel(Parcel source) {
            ChoosableImage choosableImage = new ChoosableImage();
            choosableImage.localImageUri = source.readString();
            choosableImage.localPath = source.readString();
            choosableImage.cloudKey = source.readString();
            choosableImage.thumbnailPath = source.readString();
            choosableImage.uploadPercent = source.readInt();
            choosableImage.choosed = source.readByte() != 0;
            choosableImage.uploadSucceed = source.readByte() != 0;
            choosableImage.fromCamera = source.readByte() != 0;
            choosableImage.chooseTime = source.readLong();
            return choosableImage;
        }

        @Override
        public ChoosableImage[] newArray(int size) {
            return null;
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(localImageUri);
        dest.writeString(localPath);
        dest.writeString(cloudKey);
        dest.writeString(thumbnailPath);
        dest.writeInt(uploadPercent);
        dest.writeByte((byte) (choosed ? 1 : 0));
        dest.writeByte((byte) (uploadSucceed ? 1 : 0));
        dest.writeByte((byte) (fromCamera ? 1 : 0));
        dest.writeLong(chooseTime);

    }

    @Override
    public int compareTo(ChoosableImage another) {
        if (another == null) {//按照选择时间从小到大排序
            return 1;
        } else {
            return another.chooseTime < chooseTime ? 1 : -1;
        }
    }

    @Override
    public boolean equals(Object o) {

        if (o == null || !(o instanceof ChoosableImage)) {
            return false;
        }
        ChoosableImage another = (ChoosableImage) o;
        if (StringUtil.isEmpty(another.localPath)) {
            return false;
        }
        if (another.localPath.equals(localPath)) {
            return true;
        }
        return super.equals(o);
    }

    @Override
    public String toString() {
        if (!Constants.DEBUG) {
            return super.toString();
        }
        return "ChoosableImage{" +
                "choosed=" + choosed +
                ", chooseTime=" + chooseTime +
                ", localImageUri='" + localImageUri + '\'' +
                ", localPath='" + localPath + '\'' +
                ", cloudKey='" + cloudKey + '\'' +
                ", uploadSucceed=" + uploadSucceed +
                ", uploadPercent=" + uploadPercent +
                ", fromCamera=" + fromCamera +
                ", thumbnailPath='" + thumbnailPath + '\'' +
                '}';
    }
}
